package com.grace.law.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grace.law.bean.Employee;
import com.grace.law.bean.Message;
import com.grace.law.bean.Status;
import com.grace.law.service.EmployeeService;

@RestController
public class EmployeeController {
	private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
	@Autowired
	EmployeeService employeeService;

	@RequestMapping("/emps")
	@ResponseBody
	public String getEmps(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
		PageHelper.offsetPage(pn, 5);
		List<Employee> list = employeeService.getEmps();
		PageInfo pageInfo = new PageInfo(list, 5);
		model.addAttribute("pageInfo", pageInfo);
		return "list";

	}

	@RequestMapping(value = "/emp/photo", method = RequestMethod.POST)
	@ResponseBody
	public Message uploadMultipleFileHandler(@RequestParam("file") MultipartFile[] files) throws IOException {

		Message msg = new Message();
		ArrayList<Integer> arr = new ArrayList<>();

		for (int i = 0; i < files.length; i++) {
			MultipartFile file = files[i];

			if (!file.isEmpty()) {
				InputStream in = null;
				OutputStream out = null;

				try {
					String rootPath = System.getProperty("catalina.home");
					File dir = new File(rootPath + File.separator + "tmpFiles");
					if (!dir.exists())
						dir.mkdirs();
					File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
					in = file.getInputStream();
					out = new FileOutputStream(serverFile);
					byte[] b = new byte[1024];
					int len = 0;
					while ((len = in.read(b)) > 0) {
						out.write(b, 0, len);
					}
					out.close();
					in.close();
					logger.info("Server File Location=" + serverFile.getAbsolutePath());

				} catch (Exception e) {
					arr.add(i);
				} finally {
					if (out != null) {
						out.close();
						out = null;
					}

					if (in != null) {
						in.close();
						in = null;
					}
				}
			} else {
				arr.add(i);
			}

		}

		if (arr.size() > 0) {
			msg.setStatus(Status.ERROR);
			msg.setError("Files upload fail");
			msg.setErrorKys(arr);
		} else {
			msg.setStatus(Status.SUCCESS);
			msg.setStatusMsg("Files upload success");
		}
		return msg;
	}
}
